<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/2/20
 * Time: 11:31
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Piano extends Base
{
    /**
     * 琴行列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $where = [];
            if(session('groupid') == 2){
                $where['pa_id'] = session('piano');
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_piano')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_piano')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('pa_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    /**
     * 添加
     * @return mixed|\think\response\Json
     */
    public function pianoAdd(){
        if(Request::instance()->isPost()){
            $pa_name = trim(input('pa_name'), ' ');
            $data = [
                'pa_name' => $pa_name,
                'pa_admin' => session('username'),
                'pa_time' => date('Y-m-d H:i:s'),
            ];
            $res = Db::table('up_piano')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('piano/add');
    }
    /**
     * 琴行学生
     * @return mixed|\think\response\Json
     */
    public function pianoStu(){
        $pa_id = input('pa_id');
        if(request()->isAjax()){
            $where = [
                'user_status' => ['<>',3],
                'user_type' => 1,
                'user_piano' => $pa_id
            ];
            $keyword = input('keyword');
            if(isset($keyword) && $keyword != ''){
                $where['user_username'] = ['like', '%' . $keyword . '%'];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count =  Db::table('up_u_user')
                ->where($where)
                ->count();// 获取总条数
            $lists =  Db::table('up_u_user')
                ->where($where)
                ->page($NowPage,$limits)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $this->assign('pa_id',$pa_id);
        return $this->fetch('piano/pianostu');
    }
    /**
     * 编辑
     * @return mixed|\think\response\Json
     */
    public function pianoEdit(){
        if(Request::instance()->isPost()){
            $pa_id = input('pa_id');
            $pa_name = trim(input('pa_name'), ' ');
            $data = [
                'pa_name' => $pa_name,
                'pa_admin' => session('username'),
                'pa_time' => date('Y-m-d H:i:s'),
            ];
            $res = Db::table('up_piano')->where('pa_id', $pa_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $pa_id = input('pa_id');
        // banner
        $info = Db::table('up_piano')
            ->where('pa_id', $pa_id)
            ->find();
        $this->assign([
            'pa_id' => $pa_id,
            'info' => $info
        ]);
        return $this->fetch('piano/edit');
    }
    /**
     * 删除
     * @return \think\response\Json
     */
    public function pianodel(){
        $axe_id = input('id');
        $res = Db::table('up_piano')->where('pa_id', $axe_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 200, 'msg' => '失败']);
        }
    }
    //-------------------------------------------------------------------------
}